package com.example.tellwin.wxapi;


import android.content.Intent;
import android.os.Bundle;

import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.base.BaseActivity;
import com.example.tellwin.common.Common;
import com.example.tellwin.event.PayResultEvent;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Common.WeChatPayId);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        LogUtils.e("req之前 = " + req.toString());
    }

    @Override
    public void onResp(BaseResp resp) {
        LogUtils.d( resp);
        int code = resp.errCode;
        switch (code){
            case 0://支付成功后的界面
                //返回主页面 然后在跳转至订单页面
                EventBus.getDefault().post(new PayResultEvent(true));
                break;
            case -1:
                EventBus.getDefault().post(new PayResultEvent(false));
                break;
            case -2://用户取消支付后的界面
                EventBus.getDefault().post(new PayResultEvent(false));
                break;
        }
        this.finish();
    }
}